package com.sdleyou.treepro.business.api.certificate;

import com.sdleyou.treepro.common.model.utils.ResultVOUtil;
import com.sdleyou.treepro.common.model.vo.ResultVO;
import com.sdleyou.treepro.common.service.CertificateManager;
import com.sdleyou.treepro.common.service.UserFriendManager;
import com.sdleyou.treepro.common.service.UserManager;
import com.sdleyou.treepro.common.utils.HttpClientUtil;
import com.sdleyou.treepro.system.version.ApiVersion;
import net.sf.json.JSONObject;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CertificateController {

    @Autowired
    private CertificateManager certificateManager;
    @Autowired
    private UserManager userManager;

    /**
     * 用户证书信息
     */
    @ApiVersion(1)
    @PostMapping("/{version}/certificate/info")
    public ResultVO certificateInfo(@RequestParam("user_id") String userId,
                               @RequestParam("type") String type) {
        return ResultVOUtil.success(certificateManager.getCertificateInfo(userId, type));
    }

    /**
     * 用户证书信息
     */
    @ApiVersion(1)
    @PostMapping("/{version}/certificate/add")
    public ResultVO add(@RequestParam("user_id") String userId,
                                    @RequestParam("type") String type) {
        return ResultVOUtil.success(certificateManager.getCertificateInfo(userId, type));
    }

    /**
     * 用户证书信息
     */
    @ApiVersion(1)
    @PostMapping("/test")
    public ResultVO test() {
        //certificateManager.updateCertificate();
        return ResultVOUtil.success(userManager.getSharePoster("o76Hs4vuj9tlmWbWP5nWHRJjf_BA"));
//        String url = "http://www.yiwuzhishu.cn/index.php/Home/Api/addCreateZsTy";
//        Map<String, String> param = new HashMap<>();
//        param.put("name", "雷浩");
//        param.put("sfz", "2550");
//        param.put("phone", "18603413352");
//        param.put("num", "3");
//        param.put("code", "140100");
//        param.put("leibie", "1");
//        // 发送请求
//        String postResult = HttpClientUtil.doPost(url, param);
//        System.out.println(postResult);
//        JSONObject jsonObject = JSONObject.fromObject(postResult);
//        // 获取参数返回的
//        String result = jsonObject.get("result").toString();
//        if("ok".equals(result)){
//            JSONObject message = jsonObject.getJSONObject("message");
//            int type = message.getInt("type"); //1 荣誉证书 2、义务证书
//            System.out.println(type);
//            String img = message.getString("url");
//            if(!TextUtils.isEmpty(img) && img.contains(",")){
//                String[] split = img.split(",");
//                img = split[split.length-1];
//            }
//            System.out.println(img);
//        }else{
//            return null;
//        }
//        return ResultVOUtil.success();
    }
}
